package com.zhongxing.hdy.spritel_hdy.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.zhongxing.hdy.spritel_hdy.R;
import com.zhongxing.hdy.spritel_hdy.adapter.ChongZhiAdapter;
import com.zhongxing.hdy.spritel_hdy.bean.ChongZhiInfo;
import com.zhongxing.hdy.spritel_hdy.util.AnimUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/7/2 0002.
 */

public class ChongZhiView extends BaseView {
    private Dialog dialog;
    private ImageView iv_close;
    private FrameLayout fl_chongzhi;
    private ListView lv_chongzhi;

    public ChongZhiView(Context context) {
        super(context);
        setContentView(R.layout.view_zhongzhi);

        initUI();
		initCZSAdapter();
    }

    @Override
    protected void initUI() {
        iv_close = (ImageView) findViewById(R.id.iv_close);
        AnimUtil.addOnTouchListener(iv_close, 0.5f, 0.5f, new AnimUtil.AnimListener() {
            @Override
            public void down(View view) {

            }

            @Override
            public void move(View view) {

            }

            @Override
            public void up(View view) {
                dialog.dismiss();
            }
        });

        fl_chongzhi = (FrameLayout) findViewById(R.id.fl_chongzhi);
        AnimUtil.addOnTouchListener(fl_chongzhi, 0.5f, 0.5f, null);

        lv_chongzhi = (ListView) findViewById(R.id.lv_chongzhi);
    }

    private void initCZSAdapter() {
        ChongZhiAdapter chongZhiAdapter = new ChongZhiAdapter(context, new ArrayList<ChongZhiInfo>());
        lv_chongzhi.setAdapter(chongZhiAdapter);
        //lv_chongzhi.setVisibility(View.GONE);
        //setListViewHeightBasedOnChildren(lv_chongzhi);
    }

    public void show() {
        if (dialog == null) {
            dialog = new Dialog(context, R.style.MyDialog);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(-2, -2);
            dialog.setContentView(view, params);
        }
        dialog.show();
    }

    //定义函数动态控制listView的高度
    public void setListViewHeightBasedOnChildren(ListView listView) {
        //获取listview的适配器
        ListAdapter listAdapter = listView.getAdapter();
        //item的高度
        int itemHeight = 46;
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            totalHeight += Dp2Px(context,itemHeight)+listView.getDividerHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight;
        listView.setLayoutParams(params);
    }

    //定义一个函数将dp转换为像素
    public int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

}
